
/**
*apiName: http api 里面的接口名字
*apiData: 接口参数
*/
function Refresh(vm,{ apiData,apiName,objectName,list,isClearList,hasLoading,storageName,id }){
	
	/** 构造实例的时候传进来的参数 */
	this.apiData = apiData ? apiData : {};
	
	/** 接口名字 */
	this.apiName = apiName;
	
	/** 下啦刷新的时候是否清空列表，瀑布流组件需要清空列表才能重新渲染 */
	this.isClearList = isClearList ? isClearList : false;
	
	/** 分页参数 */
	this.pageAO = {
		pageNum: 1,
		pageSize: 10
	}
	
	this.id = id;
	
	/** 默认根据该字段去重 */
	this.objectName = objectName;
	
	/** 列表数据 */
	this.list = list ? list : [];
	
	/** 加载状态 */
	this.status = 'loadmore';
	
	/** 列表总数 */
	this.total = 0;
	
	/** 是否在列表结束的时候关闭骨架平，默认是 */
	this.hasLoading = typeof(hasLoading) == 'boolean' ? hasLoading : true;
	
	this.refresh = () => {};
	
	/** 页面路径 */
	// this.pageName = getCurrentPages()[getCurrentPages().length - 1].route;
	
	/** 请求接口 */
	this.onRequest = async () => {
		this.status = 'loading'
		let res = await vm.$u.api[this.apiName]({
		    ...this.pageAO,
			...this.apiData
		},this.id);
		return res;
	}
	
	/** 下啦刷新 */
	this.onRefresh = async () => {		
		if(this.status == 'loading') return;
		this.status = 'loading';
		this.pageAO.pageNum = 1;
		try{
			this.refresh();
			if(isClearList) this.list = [];
			let res = await this.onRequest();
			// console.log('233333',res)
			this.list = res.rows;
			this.total = res.total;
			if(this.list.length < this.pageAO.pageSize){
				this.status = 'nomore';
			}else{
				//这一页的数据已经满了，翻页
				this.pageAO.pageNum++;
				this.status = 'loadmore';
			}
			
			//关闭骨架屏
			if(this.hasLoading) {
				setTimeout(() => {
					vm.mxLoading = false
				},500)
			};
			return true;
		}catch(e){
			//TODO handle the exception
			// console.log(e)
			uni.showToast({
				icon: 'none',
				title: '网络出错'
			})
			this.status = 'loadmore';
		}
	}
	
	/** 上啦加载 */
	this.onLoadmore = async () => {
		if(this.status == 'nomore' || this.status == 'loading') return;
		this.status = 'loading';
		try{
			let res = await this.onRequest();
			if(res.rows.length != res.total) this.pageAO.pageNum++;
			
			//列表去重
			let list = this.noRepeat({
				arrayA: this.list,
				arrayB: res.rows,
				compareCount: 30,
				objectName: this.objectName
			});
			if(list.length == 0) this.status = 'nomore';
			else this.status = 'loadmore';
			this.list.push(...res.rows);
			console.log(this.status)
			return true;
		}catch(e){
			//TODO handle the exception
			console.log(e)
			uni.showToast({
				icon: 'none',
				title: '网络出错'
			})
			this.status = 'loadmore';
		}
	}
	
	//传入两个数组去除重复根据objectName字段去除重复
	this.noRepeat = ({
		arrayA, //已经存在的数组
		arrayB, //分页获取到的数组，要跟arrayA比较去除重复项
		compareCount, //arrayA可能有很多条，不可能全部循环去重。arrayA数组的最后一条往前数
		objectName //字符串例如‘info.id：每一项下面的info字段下面的id字段’，当arrayA和arrayB是数组对象的时候传，拿某个字段来判断是否重复项
	}) => {
		///拿最后的compareCount条去去除重复
		if (arrayA.length > compareCount) var c = arrayA.slice(arrayA.length - compareCount, arrayA.length);
		else var c = [...arrayA];
		var d = arrayB.filter(bItem => {
			return !c.find(cItem => {
				if (objectName) {
					var nameArr = objectName.split('.');
					let objC = cItem[nameArr[0]]
					let objB = bItem[nameArr[0]]
					for (let i = 1; i < nameArr.length; i++) {
						objC = objC[nameArr[i]];
						objB = objB[nameArr[i]];
					}
					return objC == objB;
				} else {
					return cItem == bItem;
				}
			});
		})
		// console.log('分页接口返回的去重后的列表',d)
		//返回已经去重的数组
		return d;
	}
}

export default Refresh;